const _ = require('lodash')
const koa_router = require('koa-router')();
const router_config = require('../core/router');
const requireAll = require('require-all');
const path = require('path');

var controllers = requireAll({
    dirname: path.join(__dirname, '../core/controllers'),
    filter: /(.+)\.controller\.js$/
});

function loop(map, router = '') {
    _.forEach(map, (value, key) => {
        if (_.isObject(value)) {
            loop(value, router + key);
        } else {
            if (_.isString(value)) {
                const controller = value.split('.')[0];
                const fn = value.split('.')[1];
                try {
                    koa_router[key](router, global.simpleBind(controllers[controller][fn], controllers[controller]));
                } catch(err) {
                    
                }
            }
        }
    });
}

loop(router_config);

module.exports = koa_router;